using AutoMapper;
using BaseData.ReadApi.Appcontext.Command.Ram_Files;
using MediatR;
using SmartTracking.Domain.HQX;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;

namespace BaseData.ReadApi.Appcontext.CommandHandler.Ram_Files
{
    /// <summary>
    /// 种公档案查询命令处理器
    /// </summary>
    public class GetRamFileByIdCommandHandler : IRequestHandler<GetRamFileByIdCommand, ApiResult<RamFile>>
    {
        private readonly IBaseRepository<RamFile> _ramRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<GetRamFileByIdCommandHandler> _logger;

        public GetRamFileByIdCommandHandler(
            IBaseRepository<RamFile> ramRepository,
            IMapper mapper,
            ILogger<GetRamFileByIdCommandHandler> logger)
        {
            _ramRepository = ramRepository;
            _mapper = mapper;
            _logger = logger;
        }

        /// <summary>
        /// 处理种公档案查询命令
        /// </summary>
        /// <param name="request">种公档案查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>查询结果</returns>
        public async Task<ApiResult<RamFile>> Handle(GetRamFileByIdCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 验证请求参数
                if (request == null)
                {
                    _logger.LogWarning("种公档案查询请求参数为空");
                    return new ApiResult<RamFile>
                    {
                        Code = ApiEnum.查询失败,
                        Message = "请求参数不能为空",
                        data = default
                    };
                }

                // 验证数据
                if (!request.Validate(out string errorMessage))
                {
                    _logger.LogWarning($"种公档案查询数据验证失败: {errorMessage}");
                    return new ApiResult<RamFile>
                    {
                        Code = ApiEnum.查询失败,
                        Message = errorMessage,
                        data = default
                    };
                }

                // 根据ID查询种公档案
                var ramFile = await _ramRepository.GetByIdAsync(request.RamId);
                
                if (ramFile == null)
                {
                    _logger.LogWarning($"未找到ID为 {request.RamId} 的种公档案");
                    return new ApiResult<RamFile>
                    {
                        Code = ApiEnum.查询失败,
                        Message = "未找到指定的种公档案",
                        data = default
                    };
                }

                // 检查是否已被删除
                if (ramFile.IsDeleted)
                {
                    _logger.LogWarning($"ID为 {request.RamId} 的种公档案已被删除");
                    return new ApiResult<RamFile>
                    {
                        Code = ApiEnum.查询失败,
                        Message = "种公档案已被删除",
                        data = default
                    };
                }

                _logger.LogInformation($"成功查询种公档案，ID: {request.RamId}");
                return new ApiResult<RamFile>
                {
                    Code = ApiEnum.查询成功,
                    Message = "种公档案查询成功",
                    data = ramFile
                };
            }
            catch (ArgumentException ex)
            {
                _logger.LogWarning($"参数验证异常: {ex.Message}");
                return new ApiResult<RamFile>
                {
                    Code = ApiEnum.查询失败,
                    Message = ex.Message,
                    data = default
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"查询种公档案时发生系统异常: {ex.Message}");
                return new ApiResult<RamFile>
                {
                    Code = ApiEnum.查询失败,
                    Message = "系统异常，请联系管理员",
                    data = default
                };
            }
        }
    }
}